package com.whpost.service.service.cwqf;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.dao.cwqf.TQfkbBkDao;
import com.whpost.service.entity.cwqf.TQfkbBk;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TQfkbBkServices extends BaseServices<TQfkbBkDao, TQfkbBk> {

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String curpage = "",pagesize="",cxmc="",sfjm="";
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("cxmc")) {
                cxmc = jsonObject.getString("cxmc");
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            Page<TQfkbBk> page = new Page<>(Integer.parseInt(curpage),  Integer.MAX_VALUE);

            ListInfo<TQfkbBk> tQfkbBkListInfo = new ListInfo<>();
            QueryWrapper<TQfkbBk> tQfkbBkQueryWrapper = new QueryWrapper<TQfkbBk>();
            if (!StringUtils.isEmpty(cxmc)){
                tQfkbBkQueryWrapper.like("name",cxmc);
            }
            tQfkbBkQueryWrapper.orderByAsc("xh");
            tQfkbBkQueryWrapper.eq("state","1");

            IPage<TQfkbBk> tQfkbBkIPage = getBaseMapper().selectPage(page,tQfkbBkQueryWrapper);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(tQfkbBkIPage.getTotal());
            pageInfo.setCurPage(tQfkbBkIPage.getCurrent());
            pageInfo.setPageSize(tQfkbBkIPage.getSize());
            pageInfo.setPageTotal(tQfkbBkIPage.getPages());

            tQfkbBkListInfo.setPageInfo(pageInfo);
            tQfkbBkListInfo.setListInfo(tQfkbBkIPage.getRecords());
            tQfkbBkListInfo.setUserId(loginId);//登录人员id，可以不设置也行

            //加密返回给前端
            if ("1".equals(sfjm)){
                ResultInfo resultInfo = new ResultInfo(0, "成功返回！",tQfkbBkListInfo);
                return backRtn(resultInfo,loginId);
            }else{
                //非加密,返回给其它方法使用
                return new ResultInfo(0, "成功返回！",tQfkbBkListInfo);
            }
        }
    }
}
